package com.project.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.dto.AcceptanceAllDTO;
import com.project.dto.AddInventoryDTO;
import com.project.entity.AcceptanceItems;
import com.project.entity.AcceptanceRecords;
import com.project.dto.AcceptanceDTO;
import com.project.dto.InspectionQueryDTO;
import com.project.mapper.AcceptanceItemMapper;
import com.project.mapper.AcceptanceMapper;
import com.project.entity.Inventory;
import com.project.exception.ForbiddenException;
import com.project.service.InventoryService;
import com.project.vo.*;

import com.project.result.PageResult;
import com.project.service.AcceptanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

@Service
public class AcceptanceServiceImpl extends ServiceImpl<AcceptanceMapper, AcceptanceRecords> implements AcceptanceService {

    @Autowired
    private AcceptanceMapper acceptanceMapper;

    @Autowired
    private AcceptanceItemMapper acceptanceItemMapper;

    @Autowired
    private InventoryService inventoryService;


    @Override
    public AcceptanceVO get(Long id) {
        // 通过新的方法获取验收记录基本信息
        AcceptanceVO acceptanceVO = acceptanceMapper.getById(id);

        // 获取验收项详情
        List<AcceptanceItemsVO> items = acceptanceMapper.getItems(id);
        for (AcceptanceItemsVO item : items) {
            Integer status1 = item.getStatus();
            // 添加空值检查，避免 NullPointerException
            if (status1 != null) {
                switch (status1) {
                    case 0:
                        item.setStatusName("合格");
                        break;
                    case 1:
                        item.setStatusName("不合格");
                        break;
                }
            }
        }
        acceptanceVO.setItems(items);
        return acceptanceVO;
    }

    @Override
    public PageResult<AcceptancePageVO> listall(InspectionQueryDTO inspectionQueryDTO) {
        // 获取所有数据
        List<AcceptancePageVO> allList = acceptanceMapper.listall(inspectionQueryDTO);

        // 手动分页
        int page = inspectionQueryDTO.getPage() != null ? inspectionQueryDTO.getPage() : 1;
        int size = inspectionQueryDTO.getSize() != null ? inspectionQueryDTO.getSize() : 10;
        int fromIndex = (page - 1) * size;
        int toIndex = Math.min(fromIndex + size, allList.size());

        // 获取当前页数据
        List<AcceptancePageVO> pageList = allList.stream()
                .skip(fromIndex)
                .limit(size)
                .collect(Collectors.toList());

        // 构造PageResult
        PageResult<AcceptancePageVO> pageResult = new PageResult<>(allList.size(), pageList);
        return pageResult;
    }

    @Override
    public AcceptanceVO getInspectionSummary(String acceptanceNo) {
        AcceptanceVO acceptanceVO = new AcceptanceVO();
        AcceptanceRecords acceptance = acceptanceMapper.getByAccId(acceptanceNo);

        if (acceptance != null) {
            acceptanceVO.setAcceptanceId(acceptance.getAcceptanceId() != null ? acceptance.getAcceptanceId().longValue() : null);
            acceptanceVO.setAcceptanceNo(acceptance.getAcceptanceNo());
            acceptanceVO.setInspectorId(acceptance.getInspectorId());
            String inspectorName = acceptanceMapper.getByUserId(acceptance.getInspectorId());
            acceptanceVO.setInspectorName(inspectorName);
            acceptanceVO.setStatus(acceptance.getStatus());

            // 添加空值检查，避免 NullPointerException
            if (acceptance.getStatus() != null) {
                switch (acceptance.getStatus()) {
                    case 0:
                        acceptanceVO.setStatusName("待验收");
                        break;
                    case 1:
                        acceptanceVO.setStatusName("合格");
                        break;
                    case 2:
                        acceptanceVO.setStatusName("不合格");
                        break;
                }
            }
        }


        Integer acceptanceId = acceptance.getAcceptanceId();
        Long id = acceptanceId.longValue();
        List<AcceptanceItemsVO> items = acceptanceMapper.getItems(id);
        for (AcceptanceItemsVO item : items) {
            Integer status1 = item.getStatus();
            // 添加空值检查，避免 NullPointerException
            if (status1 != null) {
                switch (status1) {
                    case 0:
                        item.setStatusName("合格");
                        break;
                    case 1:
                        item.setStatusName("不合格");
                        break;
                }
            }
        }
        acceptanceVO.setItems(items);
        return acceptanceVO;
    }

    @Override
    public InspectionSummaryVO getInspectionStatistics(LocalDate start, LocalDate end) {
        return acceptanceMapper.getInspectionStatistics(start, end);

    }


    @Override
    @Transactional
    public void create(AcceptanceAllDTO acceptanceAllDTO) {

        AcceptanceRecords acceptanceRecords = acceptanceAllDTO.getAcceptanceRecords();
        acceptanceMapper.insert(acceptanceRecords);
        Integer acceptanceId = acceptanceRecords.getAcceptanceId();
        List<AcceptanceItems> acceptanceItems = acceptanceAllDTO.getAcceptanceItems();
        for (AcceptanceItems acceptanceItem : acceptanceItems) {

            acceptanceItem.setAcceptanceId(acceptanceId);
            acceptanceItemMapper.insert(acceptanceItem);
        }



        List<AddInventoryDTO> inventoryList = acceptanceItemMapper.getall(acceptanceId);
        for (AddInventoryDTO addInventoryDTO : inventoryList) {
            inventoryService.addInventory(addInventoryDTO);
        }

    }

    @Override
    public void updateStatus(Long id, Integer status) {
        acceptanceMapper.updateStatus(id, status);
    }


}